package com.yds.dccjll.lib.base.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yds.dccjll.lib.base.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dccjll on 2017/3/22.
 * 第二个简单的的RecyclerView适配器
 * 适合有二个风格的列表
 */

@SuppressWarnings("unchecked")
public abstract class SimpleExternalRVAdapter<T,M> extends RecyclerView.Adapter {
    private Context context;
    private List data;
    private int tItemLayoutResId;
    private int mItemLayoutResId;
    private int nullItemLayoutResId = R.layout.item_default_empty;

    private static final int VIEW_TYPE_NULL = -1;
    public static final int VIEW_TYPE_T = 1;
    public static final int VIEW_TYPE_M = 2;

    public SimpleExternalRVAdapter(Context context, List data, int tItemLayoutResId, int mItemLayoutResId) {
        if (context == null) {
            throw new IllegalArgumentException("context is null");
        }
        this.context = context;
        this.data = data;
        this.tItemLayoutResId = tItemLayoutResId;
        this.mItemLayoutResId = mItemLayoutResId;
    }

    public SimpleExternalRVAdapter(Context context, List data, int tItemLayoutResId, int mItemLayoutResId, int nullItemLayoutResId) {
        if (context == null) {
            throw new IllegalArgumentException("context is null");
        }
        this.context = context;
        this.data = data;
        this.tItemLayoutResId = tItemLayoutResId;
        this.mItemLayoutResId = mItemLayoutResId;
        if (nullItemLayoutResId != -1) {
            this.nullItemLayoutResId = nullItemLayoutResId;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_T){
            return new RVViewHolder(LayoutInflater.from(context).inflate(tItemLayoutResId, parent, false));
        } else if (viewType == VIEW_TYPE_M){
            return new RVViewHolder(LayoutInflater.from(context).inflate(mItemLayoutResId, parent, false));
        } else {
            return new RVViewHolder(LayoutInflater.from(context).inflate(nullItemLayoutResId, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_T && data.size() > 0){
            onBindTViewHolder(this, (RVViewHolder) holder, position, (T) data.get(position), data);
        } else if (getItemViewTypeByPosition(position) == VIEW_TYPE_M && data.size() > 0){
            onBindMViewHolder(this, (RVViewHolder) holder, position, (M) data.get(position), data);
        } else {
            if (nullItemLayoutResId == R.layout.item_default_empty) {
                onBindDefaultNullViewHolder(this, (TextView) ((RVViewHolder)holder).findViewById(R.id.emptyTv));
                return;
            }
            onBindAssignedNullViewHolder(this, (RVViewHolder) holder);
        }
    }

    @Override
    public int getItemCount() {
        return data == null || data.size() == 0 ? 1 : data.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (data == null || data.size() == 0) {
            return VIEW_TYPE_NULL;
        }
        return getItemViewTypeByPosition(position);
    }

    public abstract void onBindTViewHolder(RecyclerView.Adapter adapter, RVViewHolder rvViewHolder, int position, T entry, List<T> data);

    public abstract void onBindMViewHolder(RecyclerView.Adapter adapter, RVViewHolder rvViewHolder, int position, M entry, List<M> data);

    public abstract void onBindAssignedNullViewHolder(RecyclerView.Adapter adapter, RVViewHolder rvViewHolde);

    public abstract void onBindDefaultNullViewHolder(RecyclerView.Adapter adapter, TextView defaultNullTextView);

    public abstract int getItemViewTypeByPosition(int position);

    public Object getItem(int position) {
        return data == null || data.size() == 0 ? null : data.get(position);
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public void flush(List data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addFlush(Object item) {
        if (data == null) {
            data = new ArrayList<>();
        }
        data.add(item);
        notifyDataSetChanged();
    }

    public void update(Object item){
        int position = data.indexOf(item);
        data.add(position+1,item);
        data.remove(position);
        notifyDataSetChanged();
    }

    protected class RVViewHolder extends RecyclerView.ViewHolder {

        private SparseArray<View> viewList;
        private View itemView;

        RVViewHolder(View itemView) {
            super(itemView);
            viewList = new SparseArray<>();
            this.itemView = itemView;
        }

        public View findViewById(int viewResId) {
            View view = viewList.get(viewResId);
            if (view == null) {
                view = itemView.findViewById(viewResId);
                viewList.put(viewResId, view);
            }
            return view;
        }
    }
}